/**
 * Created by sunkejun on 2015/10/15.
 */

$(document).ready(function(){
    userinfo.userPhotoInit();
    console.log(CropAvatar);
    var cvate = new CropAvatar($('#openUploadBtn'));
    cvate.setSaveFolder("photos");
    cvate.setAttr("isphoto",1);
    cvate.callback(userinfo.uploadPhotoSuccess);
})

var userinfo  = (function ($) {
    //...
    var U = {};
    U.saveMessage = function(){
        $('#com_message').html("");
        $.ajax({
            url:  "/admin/userinfo/savemessage",
            type:'POST',
            dataType:'JSON',
            data: $('#userMsgForm').serialize(),
            success:function(response){
                if(response.code ==500){
                    $('#com_message').attr("class","text-danger");
                    $('#com_message').html(response.res_data);
                }else{
                    openToastr.open(1,response.res_data);
                }
            }
        });
    }

    U.retPassword = function(oldpwd,npwd){
        $('#pw_message').html("");
        $.ajax({
            url:  "/admin/userinfo/resetpassword",
            type:'POST',
            dataType:'JSON',
            data: $('#passwordform').serialize(),
            success:function(response){
                if(response.code ==500){
                    $('#pw_message').attr("class","text-danger");
                    $('#pw_message').html(response.res_data);
                }else{
                    openToastr.open(1,response.res_data);
                }
            }
        });
    }
    U.userPhotofileChange = function(files){
        if(files.length)
        {
            var file = files[0];
            var reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = function()
            {
                $('#userpto_image').attr("src",this.result);
                U.userPhotoInit();
            };

        }
    }

    U.userPhotoInit = function(){


    }
    U.uploadPhotoSuccess = function(data,target){
        //  {entension: "jpg", base_name: "img065439", file_path: "uploads/photos/20151017/", p_tag: "_46x46"}
        var url = data.res_data.full_file_path;
        $('#openUploadBtn').attr("src",url);
        $('#navigation_headimg').attr("src",url);
        target.closeModal();
        //保存头像进入数据库
        $.ajax({
            url:  "/admin/userinfo/savephoto",
            type:'POST',
            dataType:'JSON',
            data: {url:url},
            success:function(response){
                if(response.code ==500){
                    $('#pw_message').attr("class","text-danger");
                    $('#pw_message').html(response.res_data);
                }else{
                    openToastr.open(1,response.res_data);
                }
            }
        });

    }
    U.changePhoto = function(){

    }
    return U;
})(jQuery);